package com.example.administrator.guangdon2021_2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.nle.mylibrary.forUse.mdbus4150.MdBus4150RelayListener;
import com.nle.mylibrary.forUse.mdbus4150.MdBus4150SensorListener;
import com.nle.mylibrary.forUse.mdbus4150.Modbus4150;
import com.nle.mylibrary.transfer.DataBusFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
    private TextView tv_label;
    private ToggleButton btn_open;
    private Modbus4150 modbus4150;
    private Integer a=0;
    private Boolean flag=false;
    private static Mythread mythread;
    private Spinner sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

        btn_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (btn_open.isChecked()){
                    flag=true;
                    modbus4150 = new Modbus4150(DataBusFactory.newSocketDataBus("172.18.14.201", 4196));
                    mythread=new Mythread();
                    mythread.start();
                }else {
                    flag=false;
                    modbus4150.stopConnect();
                }
            }
        });
    }
    private void init(){
        tv_label=findViewById(R.id.tv_lable);
        btn_open=findViewById(R.id.btn_open);
        sp =  findViewById(R.id.textView);

    }
    private class Mythread extends Thread{
        @Override
        public void run() {
            super.run();
            while (flag){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    modbus4150.getVal(0, new MdBus4150SensorListener() {
                        @Override
                        public void onVal(int val) {
                            if(val==1){
                                Log.i("main", a+"亮");
                                if(a==0){
                                    Date now = new Date();
                                    SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
                                    tv_label.append(ft.format(now)+" 检测到红外对射信息,警示灯亮。\n");
                                    Toast.makeText(MainActivity.this,"text",Toast.LENGTH_SHORT).show();
                                    //发送值
                                    a=1;
                                    Log.i("main", a+"1");
                                }
                                try {
                                    modbus4150.openRelay(7, new MdBus4150RelayListener() {
                                        @Override
                                        public void onCtrl(boolean isSuccess) {
                                        }
                                    });
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }else if(val==0) {
                                Log.i("main", a+"关");
                                if(a==1){
                                    Date now = new Date( );
                                    SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
                                    tv_label.append(ft.format(now)+" 未检测到红外对射信息,警示灯灭。\n");
                                    a=0;
                                    Log.i("main", a+"2");
                                }
                                try {
                                    modbus4150.closeRelay(7, new MdBus4150RelayListener() {
                                        @Override
                                        public void onCtrl(boolean isSuccess) {

                                        }
                                    });
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
